var test = 1;//测试版本
var url = 'http://www.longtcp138.com/mobile/interface/';//统一请求地址
var timeStamp = new Date().getTime();
var tokens = timeStamp + '4baea6bc1bd66a7e00aa778d4aca6720';//token
var token = hex_md5(tokens);//md5加密
// console.log(token)

var jsonObj = {timeStamp:timeStamp,token:token,test:test};
// 封装ajax
function $ajax(url,type,postData,successFn,errorFn){
	var _index;
	$.ajax({
		url:url,
		type:type,
		data:postData,
		beforeSend:function(){
			_index = layer.load(1, {
				shade: [0.3,'#000'] 
			});
		},
		success:function(data){
			
			if(data.code == 200){
				
				successFn && successFn(data)
				
			}else{
				
				errorFn && errorFn(data)
			}
		},
		error: function (data) {
            
			layer.msg('加载失败');
		},
		complete:function(){
			layer.close(_index); 
		}
	})
}

// 设置cooike
function setCookie(cname,cvalue,exdays){
  var d = new Date();
  d.setTime(d.getTime()+(exdays*24*60*60*1000));
  var expires = "expires="+d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}
// 获取cookie
function getCookie(cname){
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) 
  {
    var c = $.trim(ca[i]);
    if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
  return "";
}
//清除cookie
function clearCookie(name) {  
    setCookie(name, "", -1);  
} 
// 检查cookie
function checkCookie(){
	var uid=getCookie("uid");
	if (uid!=""){
			alert("欢迎 " + user + " 再次访问");
	}else {
		uid = prompt("请输入你的名字:","");
        if (user!="" && user!=null){
            setCookie("username",user,30);
        }
	}
}
// 开奖历史
function onBetdetails(){
    window.location.href = '../lottery.html'
}
//正则截取url上的参数 
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if ( r != null ){
		 return unescape(r[2]);
	}else{
		 return null;
	} 
}
//客服链接
function onlineService(link) {

	var openUrl = link;//弹出窗口的url
	var iWidth=800; //弹出窗口的宽度;
	var iHeight=600; //弹出窗口的高度;
	var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
	var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
	window.open(openUrl,"","height="+iHeight+", width="+iWidth+", top="+iTop+", left="+iLeft);return false;
} 
//单纯分钟和秒倒计时
function resetTime(time,attr){
    var timer=null;
    var t=time;
    var m=0;
    var s=0;
    m=Math.floor(t/60%60);
    m<10&&(m='0'+m);
    s=Math.floor(t%60);
    
    function countDown(){
     s--;
     s<10&&(s='0'+s);
     
     if(s.length>=3){
      s=59;

      m<10 ? m="0"+(Number(m)-1) : m=(Number(m)-1);
     }
     if(m.length>=3){
      m='00';
      s='00';
      clearInterval(timer);
     }
     
     if(m == 00 && s == 00){
        attr.text('已封盘');
        
     }else{
        attr.text(m+":"+s);
     }
    }
    countDown()
    timer=setInterval(countDown,1000);
  }

//qq链接
function openQQ(qqNum) {
	window.location.href="tencent://message/?uin=" + qqNum + "&Site=qq&Menu=yes";
}
// 数组去重
Array.prototype.distinct = function (){
    var arr = this,
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i] == arr[j]){
                arr.splice(j,1);
                len--;
                j--;
            }
        }
    }
    return arr;
};
// 修改下注内容
function action(type){
    var obj = jsonObj;
    obj.type = type;//必发六合彩
    
    $ajax(
      url+'dead_awar',
      'post',
      obj,
      function(data){
        console.log(data)
        var countDown = data.data.countDown;//倒计时
        $('.a-name').text(countDown.lotteryName);
        $('.f-qi').text(countDown.issue);
        resetTime(countDown.countDown,$('#f-time'))//封盘倒计时
        var openResult = data.data.openResult;//上期开奖
        console.log(countDown)
        $('#s-qi').text(openResult.issue);
        if(openResult.number){
            var numberArry = openResult.number.split(',');//开奖号码
        
            var res = '';
            for(var i = 0,iL = numberArry.length;i<iL;i++){
            res += '<i>'+numberArry[i]+'</i>'
            }
            $('#s-hao').append(res)
        }
        
  
      },
      function(data){
        layer.msg(data.msg)
      }
    )
  }
// banner
function banner(banners){
    var res = '';
    if(banners.length > 0){

        for(var i = 0 ,iL = banners.length;i<iL;i++){

            res += '<a href='+banners[i].url +'>'+
                        '<img src='+banners[i].image+' >'+
                    '</a>';

        }
        $('#lists').append(res);
    }

    layui.use('carousel', function(){
        var carousel = layui.carousel;
        //建造实例
        carousel.render({
            elem: '#container'
            ,width: '1000px' //设置容器宽度
            ,height:'445px' //设置容器高度
        });
    });
}
 // 提现
function deposit(data){
   
    $('.withdraw-btn').click(function(){
						
        if(data.data.realname && data.data.qq){  //认证qq姓名成功
            if(data.data.bankCard){//绑定银行卡 
                window.location.href =  '../withdraw.html';

            }else{//未绑定银行卡 

                layer.confirm('您还未绑定银行卡', {
                    closeBtn: 0,
                    skin: 'lay-tip',
                    title:'提示',
                    btn: ['立即绑定','取消'] //按钮
                }, function(){
                    window.location.href = '../bankcard.html'
                })
            }
        }else{
            layer.confirm('您还未认证真实姓名', {
                    closeBtn: 0,
                    skin: 'lay-tip',
                    title:'提示',
                    btn: ['立即认证','取消'] //按钮
                }, function(){
                    layer.closeAll();
                    layer.open({
                    type: 1,
                    title: false,
                    skin: 'lay-passworld', //样式类名
                    closeBtn: 0, //不显示关闭按钮
                    anim: 2,
                    area: ['450px', '280px'],
                    shadeClose: true, //开启遮罩关闭
                    content: '<div class="lay-qq">'+
                                            '<div class="layqq-content">'+
                                                '<div class="layqq-inp">'+
                                                    '<input type="text" placeholder="请输入真实姓名" id="realname">'+
                                                    '<p>*真实姓名必须与您的银行卡账户姓名相同，否则不能提现</p>'+
                                                '</div>'+
                                                '<div class="layqq-inp">'+
                                                    '<input type="text" placeholder="请输入QQ账号" id="realqq">'+
                                                    '<p>*请输入真实的QQ账号</p>'+
                                                '</div>'+
                                                '<button type="button" class="layqq-bind">绑定</button>'+
                                            '</div>'+
                                        '</div>'
                    });
                    $('.layqq-bind').click(function(){
                        jsonObj.realname = $('#realname').val();
                        jsonObj.qq = $('#realqq').val();
                        jsonObj.uid = getCookie('uid');
                        $ajax(
                            url+'rz_qq_name',
                            'post',
                            jsonObj,
                            function(data){
                                
                                layer.msg(data.msg,{time:1000},function(){
                                    window.location.reload();
                                });
                            },
                            function(data){
                                console.log(data)
                                layer.msg(data.msg)
                            }
                        );
                    })
                }, function(){
                    //点击取消
                });
        }
    })
}


// 选择银行
var bankArry = [
    '招商银行',
    '工商银行',
    '建设银行',
    '农业银行',
    '中国银行',
    '交通银行',
    '中国邮政储蓄银行',
    '中信银行',
    '兴业银行',
    '广大银行',
    '华夏银行',
    '中国民生银行',
    '广发银行',
    '上海浦东发展银行',
    '农村信用合作社',
    '农村商业银行',
    '平安银行',
    '上海银行',
    '北京银行',
    '恒丰银行',
    '渤海银行',
    '广州银行',
    '天津银行',
    '浙商银行',
    '宁波银行',
    '温州银行',
    '南京银行',
    '常熟农村商业银行',
    '微商银行',
    '重庆银行',
    '哈尔滨银行'
]

//banner
/**
 *  浮動 (預設右上 top:150)
 *  @example   $("#id").Float();
 *  @param {topSide:150,floatRight:0|1,side:5,close:ID}
 */
$.fn.Float = function(obj){
    var that = this;
    var lock = {
        topSide : 150,
        floatRight :1, 
        side : 5, //margin
        init : function(){
            var el = that, ua = navigator.userAgent;
            el.css({
                'position':'absolute',
                'z-index':'1000',
                'top': this.topSide
            });
            if (ua.toLowerCase().indexOf('360se') > -1) {
                this.isBlock = true;
            } else if (ua.toLowerCase().indexOf("theworld") > 0) {
                this.isBlock = true;
	        } else if (ua.toLowerCase().indexOf("msie 7") > 0) {
	            this.side = -1;
            }
            this.floatRight == 1 ? el.css('right',this.side) : el.css('left',this.side);
                    var locker = this;
            setInterval(function () {
                locker.lock.call(locker);
            }, 20);
            
            if(this.close != undefined){
                this.closeFloat();
            }
            if (this.floatRight == 1) {
                $(window).resize(function () {
                    $(this).scrollLeft(0);
                    el.css('right', 5);
                });
            }
        },
        lockTop : function (el, position, page, scroll, icon){
            var top = el.css('top');
            var y = scroll.top + icon.topSide,
                offsetTop = (y - parseInt(top)) / 20;
            if (this.isBlock) {
                offsetTop = (y - parseInt(top));
            }
            var topSide = parseInt(top) + offsetTop;
            if ((topSide + position.height) < page.height) {
                el.css('top',topSide );
            }
        },
        lockLeft : function (el, scroll, icon) {
            var left = el.css('left');
            var x = scroll.left + icon.side,
                offsetLeft = (x - parseInt(left)) / 20;
            el.css('left',parseInt(left) + offsetLeft);
        },
        lockRight : function (el, scroll, icon) {
            var right = el.css('right');
            var d = document;
            if (scroll.left == 0) {
                var x = icon.side,
                    offsetRight = (Math.abs(x) - Math.abs(parseInt(right))) / 20;
                el.css('right',Math.abs(parseInt(right)) + offsetRight);
            } else {
                    var x = scroll.left - icon.side,
                        offsetRight = (Math.abs(x) - Math.abs(parseInt(right))) / 20;
                    right = -(Math.abs(parseInt(right)) + offsetRight) + "px";
                    el.css('right',right);
                }
        },
        lock : function(){
             var el = that,
                position = this.currentPosition(el),
                win = this.windowDimension(),
                scroll = this.scrollPosition(),
                page = this.pageDimension(),
                icon = this;
            this.lockTop(el, position, page, scroll, icon);
            if (this.floatRight == 1) {
                this.lockRight(el, scroll, icon);
            } else {
                this.lockLeft(el, scroll, icon);
            }
            if (this.isBlock) {
                if (this.lastTop != el.css('top')) {
                    el.css('visibility',"hidden");
                    this.lastTop = el.css('top');
                } else {
                    el.css('visibility',"visible");
                }
            }
        },
        currentPosition: function (el) {
            var offset = el.offset();
            return {
               /* top: offset.top,
                left: offset.left,*/
                width: el.outerWidth(),
                height: el.outerHeight()
            };
        },
        windowDimension: function () {
            if ((typeof innerWidth != "undefined" && innerWidth != 0) && (typeof innerHeight != "undefined" && innerHeight != 0)) {
                return {
                    width: innerWidth,
                    height: innerHeight
                };
            }
            var d = document;
            return {
                width: Math.min(d.body.clientWidth, d.documentElement.clientWidth),
                height: Math.min(d.body.clientHeight, d.documentElement.clientHeight)
            };
        },
        scrollPosition: function () {
            var d = document;
            return {
                top: Math.max(d.body.scrollTop, d.documentElement.scrollTop),
                left: Math.max(d.body.scrollLeft, d.documentElement.scrollLeft)
            };
        },
        pageDimension: function () {
            var d = document;
            return {
                width: Math.max(d.body.scrollWidth, d.documentElement.scrollWidth),
                height: Math.max(d.body.scrollHeight, d.documentElement.scrollHeight)
            };
        },
        closeFloat : function(){
            that.find('#'+this.close).click(function(){
                that.hide();
            }).css('cursor','pointer');
        }
    };
    if (obj) $.extend(lock, obj);
    lock.init();
};

